package com.ruoyi.datastatistics.service.impl;

import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.Parameter;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictVO;
import com.ruoyi.common.core.enums.UserStatus;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.feign.ModulesSystemFeign;
import com.ruoyi.common.core.utils.CryptoUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.datastatistics.domain.*;
import com.ruoyi.datastatistics.feign.SysDeptService;

import com.ruoyi.datastatistics.mapper.HomepageMapper;
import com.ruoyi.datastatistics.service.HomepageService;
import com.ruoyi.common.core.utils.Global;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HomepageServiceImpl implements HomepageService {

    @Autowired
    private HomepageMapper homepageMapper;
    @Autowired
    public ModulesSystemFeign systemFeign;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private RemoteUserService remoteUserService;

    /*public Parameter setParameter(Parameter parameter){
        //根据时间查询数据表标识符
        SysDictVO sysDict = systemFeign.getSysDict(parameter.getYear());
        String tablename = Global.DATATABLEPRE + "_" + sysDict.getValue() + Global.TESTWHOLEDATANAME;
        parameter.setTablename(tablename);
        //根据dept_id查询机构信息
        SysDept sysDept = new SysDept();
        if (null != parameter.getDeptid() && !"".equals(parameter.getDeptid())){
            sysDept = sysDeptService.selectDeptById(Long.valueOf(parameter.getDeptid()));
        } else if (null != parameter.getCity() && !"".equals(parameter.getCity())) {
            sysDept = sysDeptService.selectDeptByName(parameter.getCity());
            parameter.setDeptid(String.valueOf(sysDept.getDeptId()));
        }
        parameter.setOrganizationCode(sysDept.getOrganizationcode());
        return parameter;
    }*/
    @Override
    public  Map<String,Object> gettotaldata(Parameter parameter) {
        Map<String,Object> map = new HashMap<>();
        parameter = systemFeign.setParameter(parameter);
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            HomepageResult totaldata = homepageMapper.selecttotal(parameter);
            map.put("data",totaldata);
            map.put("message",true);
            return map;
        }else {
            map.put("data",null);
            map.put("message","请在机构管理中添加该机构信息");
            return map;
        }

    }

    @Override
    public Map<String,Object> getnexttotalbyday(Parameter parameter) {
        Map<String,Object> dataMap = new HashMap<>();
        parameter = systemFeign.setParameter(parameter);
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
        List<Map<String,Object>> nexttotalbydaydata = homepageMapper.getnexttotalbyday(parameter);
        dataMap.put("data",nexttotalbydaydata);
        dataMap.put("parameter",parameter);
        dataMap.put("message",true);
        return dataMap;
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
            return dataMap;
        }
    }

    @Override
    public Object dengjixinxiData(String shenfenzhenghao, String year) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(null == year || "".equals(year)){
            year = new SimpleDateFormat("yyyy").format(new Date());
        }
        SysDictVO sysDict = systemFeign.getSysDict(year);
        String testWholeDataTableName = Global.DATATABLEPRE + "_" + sysDict.getValue() + Global.TESTWHOLEDATANAME;
        try {
            shenfenzhenghao = CryptoUtils.encryptSymmetrically(Global.COLS_AES_SECRETKEY, null, shenfenzhenghao, CryptoUtils.Algorithm.Encryption.AES_ECB_PKCS5);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<String> TestDates = homepageMapper.getTesttime(testWholeDataTableName,shenfenzhenghao);
        Collections.sort(TestDates);
        if(TestDates != null && TestDates.size() > 0) {
            map.put("success", true);
            map.put("message", TestDates);
        }else {
            map.put("success", false);
            map.put("message", "当前用户本年度无测试记录");
        }
        return map;
    }

    @Override
    public Object itemData(String shenfenzhenghao, String year, String testtime) {
        Map<String, Object> map = new HashMap<String, Object>();
        //测试项目列表list
        List<Map<String, Object>> itemnameList = new ArrayList<Map<String, Object>>();
        if(null == year || "".equals(year)){
            year = new SimpleDateFormat("yyyy").format(new Date());
        }
        SysDictVO sysDict = systemFeign.getSysDict(year);
        String testWholeDataTableName = Global.DATATABLEPRE + "_" + sysDict.getValue() + Global.TESTWHOLEDATANAME;
        try {
            shenfenzhenghao = CryptoUtils.encryptSymmetrically(Global.COLS_AES_SECRETKEY, null, shenfenzhenghao, CryptoUtils.Algorithm.Encryption.AES_ECB_PKCS5);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<Map<String, Object>> list = homepageMapper.getNumtests(testWholeDataTableName, shenfenzhenghao, testtime);
        if(list.size() > 0) {
            if (null != list.get(0).get("zongheScore") && !"".equals(list.get(0).get("zongheScore"))) {
                Map<String, Object> itemmap = new HashMap<String, Object>();
                itemmap.put("itemcode", Global.DATATABLEPRE + "_" + sysDict.getValue() + Global.GUOMINTIZHICESHINAME);
                itemmap.put("itemname", "国民体质");
                itemnameList.add(itemmap);
            }
            if (null != list.get(0).get("heartrate") && !"".equals(list.get(0).get("heartrate"))) {
                Map<String, Object> itemmap = new HashMap<String, Object>();
                itemmap.put("itemcode", Global.DATATABLEPRE + "_" + sysDict.getValue() + Global.XUEYAJINAME);
                itemmap.put("itemname", "血压");
                itemnameList.add(itemmap);
            }
            if (null != list.get(0).get("metval") && !"".equals(list.get(0).get("metval"))) {
                Map<String, Object> itemmap = new HashMap<String, Object>();
                itemmap.put("itemcode", Global.DATATABLEPRE + "_" + sysDict.getValue() + Global.GONGLVCHENAME);
                itemmap.put("itemname", "功率车二级负荷试验");
                itemnameList.add(itemmap);
            }
            if (null != list.get(0).get("ZFLJG_FAT_W") && !"".equals(list.get(0).get("ZFLJG_FAT_W"))) {
                Map<String, Object> itemmap = new HashMap<String, Object>();
                itemmap.put("itemcode", Global.DATATABLEPRE + "_" + sysDict.getValue() + Global.TICHENGFENNAME);
                itemmap.put("itemname", "体脂率");
                itemnameList.add(itemmap);
            }

        }
        if(itemnameList != null && itemnameList.size() > 0) {
            map.put("success", true);
            map.put("message", itemnameList);
        }else {
            map.put("success", false);
            map.put("message", "当前用户本年度无测试记录");
        }
        return map;
    }

    @Override
    public Object personalReport(String idcard, String datasheet, String testtime) {
        //反馈前端信息Map
        Map<String, Object> dataMap = new HashMap<String, Object>();
        //国民体质测试数据
        List<TestCorporeityResult> testCorporeityResultList = new ArrayList<>();
        //整表测试数据表
        String testWholeDataTableName = datasheet.substring(0,3) + Global.TESTWHOLEDATANAME;
        try {
            idcard = CryptoUtils.encryptSymmetrically(Global.COLS_AES_SECRETKEY, null, idcard, CryptoUtils.Algorithm.Encryption.AES_ECB_PKCS5);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //国民体质数据报告
        if(datasheet.contains(Global.GUOMINTIZHICESHINAME)) {
            //查询该受测者当日参与测评项目的最好成绩
            List<Map<String, Object>> dataList = homepageMapper.getTestCorporeityReport(testWholeDataTableName, idcard, testtime);
            ATestMember aTestMember = new ATestMember();
            try {
                aTestMember.setRealname(CryptoUtils.decryptSymmetrically(Global.COLS_AES_SECRETKEY, null, dataList.get(0).get("realname").toString(), CryptoUtils.Algorithm.Encryption.AES_ECB_PKCS5));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            aTestMember.setSex(dataList.get(0).get("sex").toString());
            aTestMember.setAge(null == dataList.get(0).get("age") || "".equals(dataList.get(0).get("age")) ? null : Float.parseFloat(dataList.get(0).get("age").toString()));
            aTestMember.setNation(null == dataList.get(0).get("nation") || "".equals(dataList.get(0).get("nation")) ? "" : dataList.get(0).get("nation").toString());
            aTestMember.setMechanismname(dataList.get(0).get("mechanismname").toString());
            for (Map<String, Object> map : dataList) {
                TestCorporeityResult testCorporeityResult = new TestCorporeityResult();
                testCorporeityResult.setTesttime(map.get("testtime").toString());
                testCorporeityResult.setTestage(Float.parseFloat(map.get("testage").toString()));
                testCorporeityResult.setItemcode(Integer.parseInt(map.get("code").toString()));
                testCorporeityResult.setZhibiaomingcheng(map.get("zhibiaomingcheng").toString());
                if ("100" == map.get("code").toString() || "100".equals(map.get("code"))) {
                    testCorporeityResult.setScore(Float.parseFloat(map.get("zongheScore").toString()));
                    testCorporeityResult.setGrade(map.get("zongheGrade").toString());
                } else {
                    String columncode = map.get("columncode").toString();
                    if (!"lfzydgttz".equals(columncode) && !"lfzydgtty".equals(columncode)) {
                        testCorporeityResult.setScore(null == map.get(columncode+"score") || "".equals(map.get(columncode+"score")) ? null : Float.parseFloat(map.get(columncode+"score").toString()));
                        testCorporeityResult.setGrade(null == map.get(columncode+"grade") || "".equals(map.get(columncode+"grade")) ? "" : map.get(columncode+"grade").toString());
                    }
                    testCorporeityResult.setTestvalue(null == map.get(columncode+"val") || "".equals(map.get(columncode+"val")) ? null : Float.parseFloat(map.get(columncode+"val").toString()));
                    testCorporeityResult.setDanwei(null == map.get("danwei") || "".equals(map.get("danwei")) ? "" : map.get("danwei").toString());

                }
                testCorporeityResult.setZhibiaofenlei(map.get("zhibiaofenlei").toString());
                testCorporeityResult.setFenleicode(map.get("fenleicode").toString());
                testCorporeityResultList.add(testCorporeityResult);
            }
            //测试完成显示全部数据报告当前数据，评价以及重点关注
            dataMap.put("ExercisePrescription", getTestOfPhysicalReport(Float.parseFloat(dataList.get(0).get("testage").toString()),testCorporeityResultList,dataList.get(0).get("xingbie").toString()));
            dataMap.put("data", testCorporeityResultList);
            dataMap.put("code", true);
            //dataMap.put("con", con);
            dataMap.put("testuser", aTestMember);
            //}

        } else {
            //查询该受测者当日血压计测试数据
            TestDataResult testDataList = homepageMapper.getTestData(testWholeDataTableName, idcard, testtime);
            ATestMember aTestMember = new ATestMember();
            //aTestMember.setRealname(testDataList.getRealname());
            try {
                aTestMember.setRealname(CryptoUtils.decryptSymmetrically(Global.COLS_AES_SECRETKEY, null, testDataList.getRealname(), CryptoUtils.Algorithm.Encryption.AES_ECB_PKCS5));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            aTestMember.setSex(testDataList.getSex());
            aTestMember.setAge(null == testDataList.getAge() || "".equals(testDataList.getAge()) ? null : Float.parseFloat(testDataList.getAge()));
            aTestMember.setNation(null == testDataList.getNation() || "".equals(testDataList.getNation()) ? "" : testDataList.getNation());
            aTestMember.setMechanismname(testDataList.getMechanismname());
            dataMap.put("data", testDataList);
            dataMap.put("code", true);
            dataMap.put("testuser", aTestMember);
        }
        return dataMap;
    }

    @Override
    public Object loginapp(String username, String password) {
        //用AES解密用户名和密码
        try {
            username = CryptoUtils.decryptSymmetrically(Global.USER_AES_SECRETKEY, null, username, CryptoUtils.Algorithm.Encryption.AES_ECB_PKCS5);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            password = CryptoUtils.decryptSymmetrically(Global.USER_AES_SECRETKEY, null, password, CryptoUtils.Algorithm.Encryption.AES_ECB_PKCS5);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Map<String,Object> map = new HashMap<>();
        // 查询用户信息
        R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
        if (R.FAIL == userResult.getCode())
        {
            map.put("status","400");
            map.put("data","用户名或密码错误");
            return map;
        }

        LoginUser userInfo = userResult.getData();
        SysUser user = userResult.getData().getSysUser();
        if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
        {
            map.put("status","400");
            map.put("data","对不起，您的账号" + username + "  已被删除");
            return map;
        }
        if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
        {
            map.put("status","400");
            map.put("data","对不起，您的账号：" + username + "   已停用");
            return map;
        }
        if (matches(user, password))
        {
            //根据用户信息查询机构信息，并返回
            Map<String,Object> temp = new HashMap<>();
            SysDept sysDept = homepageMapper.selectSysDept(user.getDeptId());
            temp.put("name",sysDept.getDeptName());
            temp.put("organizationcode",sysDept.getOrganizationcode());
            temp.put("msg","登录成功");
            map.put("status","200");
            map.put("data",temp);
            return map;
        }else {
            map.put("status","400");
            map.put("data","无机构信息或机构已停用");
            return map;
        }
    }

    @Override
    public Object personaltrend(String idcard, String year) {
        try {
            idcard = CryptoUtils.encryptSymmetrically(Global.COLS_AES_SECRETKEY, null, idcard, CryptoUtils.Algorithm.Encryption.AES_ECB_PKCS5);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        SysDictVO sysDict = systemFeign.getSysDict(year);
        String tablename = Global.DATATABLEPRE + "_" + sysDict.getValue() + Global.TESTWHOLEDATANAME;
        List<DataApp> dataAppList = homepageMapper.selectDataApp(tablename,idcard);
        List<Map<String,Object>> dataList = new ArrayList<>();
        Map<String,Object> shengaovaldata = new HashMap<>();
        Map<String,Object> zuogaovaldata = new HashMap<>();
        Map<String,Object> tizhongvaldata = new HashMap<>();
        Map<String,Object> tzzsvaldata = new HashMap<>();
        Map<String,Object> tzlvaldata = new HashMap<>();
        Map<String,Object> xiongweivaldata = new HashMap<>();
        Map<String,Object> yaoweivaldata = new HashMap<>();
        Map<String,Object> tunweivaldata = new HashMap<>();
        Map<String,Object> fhlvaldata = new HashMap<>();
        Map<String,Object> glcvaldata = new HashMap<>();
        Map<String,Object> lfzydgttvaldata = new HashMap<>();
        Map<String,Object> lfzydgttzvaldata = new HashMap<>();
        Map<String,Object> lfzydgttyvaldata = new HashMap<>();
        Map<String,Object> ajxlvaldata = new HashMap<>();
        Map<String,Object> wolivaldata = new HashMap<>();
        Map<String,Object> zongtiaovaldata = new HashMap<>();
        Map<String,Object> fwcvaldata = new HashMap<>();
        Map<String,Object> gwcvaldata = new HashMap<>();
        Map<String,Object> yfzywqzvaldata = new HashMap<>();
        Map<String,Object> zwtqqvaldata = new HashMap<>();
        Map<String,Object> bydjzlvaldata = new HashMap<>();
        Map<String,Object> xzfysvaldata = new HashMap<>();
        Map<String,Object> beilivaldata = new HashMap<>();
        Map<String,Object> ssmzzvaldata = new HashMap<>();
        Map<String,Object> ldtyvaldata = new HashMap<>();
        Map<String,Object> sjlxtvaldata = new HashMap<>();
        Map<String,Object> swmrzapvaldata = new HashMap<>();
        Map<String,Object> zphmvaldata = new HashMap<>();
        List<String> testtimelist = new ArrayList<>();
        List<String> shengaovallist = new ArrayList<>();
        List<String> zuogaovallist = new ArrayList<>();
        List<String> tizhongvallist = new ArrayList<>();
        List<String> tzzsvallist = new ArrayList<>();
        List<String> tzlvallist = new ArrayList<>();
        List<String> xiongweivallist = new ArrayList<>();
        List<String> yaoweivallist = new ArrayList<>();
        List<String> tunweivallist = new ArrayList<>();
        List<String> fhlvallist = new ArrayList<>();
        List<String> glcvallist = new ArrayList<>();
        List<String> lfzydgttvallist = new ArrayList<>();
        List<String> lfzydgttzvallist = new ArrayList<>();
        List<String> lfzydgttyvallist = new ArrayList<>();
        List<String> ajxlvallist = new ArrayList<>();
        List<String> wolivallist = new ArrayList<>();
        List<String> zongtiaovallist = new ArrayList<>();
        List<String> fwcvallist = new ArrayList<>();
        List<String> gwcvallist = new ArrayList<>();
        List<String> yfzywqzvallist = new ArrayList<>();
        List<String> zwtqqvallist = new ArrayList<>();
        List<String> bydjzlvallist = new ArrayList<>();
        List<String> xzfysvallist = new ArrayList<>();
        List<String> beilivallist = new ArrayList<>();
        List<String> ssmzzvallist = new ArrayList<>();
        List<String> ldtyvallist = new ArrayList<>();
        List<String> sjlxtvallist = new ArrayList<>();
        List<String> swmrzapvallist = new ArrayList<>();
        List<String> zphmvallist = new ArrayList<>();

        for (DataApp app : dataAppList) {
            testtimelist.add(app.getTesttime());
            shengaovallist.add(app.getShengaoval());
            zuogaovallist.add(app.getZuogaoval());
            tizhongvallist.add(app.getTizhongval());
            tzzsvallist.add(app.getTzzsval());
            tzlvallist.add(app.getTzlval());
            xiongweivallist.add(app.getXiongweival());
            yaoweivallist.add(app.getYaoweival());
            tunweivallist.add(app.getTunweival());
            fhlvallist.add(app.getFhlval());
            glcvallist.add(app.getGlcval());
            lfzydgttvallist.add(app.getLfzydgttval());
            lfzydgttzvallist.add(app.getLfzydgttzval());
            lfzydgttyvallist.add(app.getLfzydgttyval());
            ajxlvallist.add(app.getAjxlval());
            wolivallist.add(app.getWolival());
            zongtiaovallist.add(app.getZongtiaoval());
            fwcvallist.add(app.getFwcval());
            gwcvallist.add(app.getGwcval());
            yfzywqzvallist.add(app.getYfzywqzval());
            zwtqqvallist.add(app.getZwtqqval());
            bydjzlvallist.add(app.getBydjzlval());
            xzfysvallist.add(app.getXzfysval());
            beilivallist.add(app.getBeilival());
            ssmzzvallist.add(app.getSsmzzval());
            ldtyvallist.add(app.getLdtyval());
            sjlxtvallist.add(app.getSjlxtval());
            swmrzapvallist.add(app.getSwmrzapval());
            zphmvallist.add(app.getZphmval());
        }
        //身高
        if (shengaovallist != null && shengaovallist.stream().anyMatch(Objects::nonNull)) {
            shengaovaldata.put("name","身高");
            shengaovaldata.put("time",testtimelist);
            shengaovaldata.put("data",shengaovallist);
            dataList.add(shengaovaldata);
        }

        //坐高
        if (zuogaovallist != null && zuogaovallist.stream().anyMatch(Objects::nonNull)) {
            zuogaovaldata.put("name","坐高");
            zuogaovaldata.put("time",testtimelist);
            zuogaovaldata.put("data",zuogaovallist);
            dataList.add(zuogaovaldata);
        }

        //体重
        if (tizhongvallist != null && tizhongvallist.stream().anyMatch(Objects::nonNull)) {
            tizhongvaldata.put("name","体重");
            tizhongvaldata.put("time",testtimelist);
            tizhongvaldata.put("data",tizhongvallist);
            dataList.add(tizhongvaldata);
        }

        //体重指数
        if (tzzsvallist != null && tzzsvallist.stream().anyMatch(Objects::nonNull)) {
            tzzsvaldata.put("name","体重指数");
            tzzsvaldata.put("time",testtimelist);
            tzzsvaldata.put("data",tzzsvallist);
            dataList.add(tzzsvaldata);
        }

        //体脂率
        if (tzlvallist != null && tzlvallist.stream().anyMatch(Objects::nonNull)) {
            tzlvaldata.put("name","体脂率");
            tzlvaldata.put("time",testtimelist);
            tzlvaldata.put("data",tzlvallist);
            dataList.add(tzlvaldata);
        }

        //胸围
        if (xiongweivallist != null && xiongweivallist.stream().anyMatch(Objects::nonNull)) {
            xiongweivaldata.put("name","胸围");
            xiongweivaldata.put("time",testtimelist);
            xiongweivaldata.put("data",xiongweivallist);
            dataList.add(xiongweivaldata);
        }

        //腰围
        if (yaoweivallist != null && yaoweivallist.stream().anyMatch(Objects::nonNull)) {
            yaoweivaldata.put("name","腰围");
            yaoweivaldata.put("time",testtimelist);
            yaoweivaldata.put("data",yaoweivallist);
            dataList.add(yaoweivaldata);
        }

        //臀围
        if (tunweivallist != null && tunweivallist.stream().anyMatch(Objects::nonNull)) {
            tunweivaldata.put("name","臀围");
            tunweivaldata.put("time",testtimelist);
            tunweivaldata.put("data",tunweivallist);
            dataList.add(tunweivaldata);
        }

        //肺活量
        if (fhlvallist != null && fhlvallist.stream().anyMatch(Objects::nonNull)) {
            fhlvaldata.put("name","肺活量");
            fhlvaldata.put("time",testtimelist);
            fhlvaldata.put("data",fhlvallist);
            dataList.add(fhlvaldata);
        }

        //功率车
        if (glcvallist != null && glcvallist.stream().anyMatch(Objects::nonNull)) {
            glcvaldata.put("name","功率车");
            glcvaldata.put("time",testtimelist);
            glcvaldata.put("data",glcvallist);
            dataList.add(glcvaldata);
        }

        //两分钟原地高抬腿
        if (lfzydgttvallist != null && lfzydgttvallist.stream().anyMatch(Objects::nonNull)) {
            lfzydgttvaldata.put("name","两分钟原地高抬腿");
            lfzydgttvaldata.put("time",testtimelist);
            lfzydgttvaldata.put("data",lfzydgttvallist);
            dataList.add(lfzydgttvaldata);
        }

        //两分钟原地高抬左腿
        if (lfzydgttzvallist != null && lfzydgttzvallist.stream().anyMatch(Objects::nonNull)) {
            lfzydgttzvaldata.put("name","两分钟原地高抬腿左腿");
            lfzydgttzvaldata.put("time",testtimelist);
            lfzydgttzvaldata.put("data",lfzydgttzvallist);
            dataList.add(lfzydgttzvaldata);
        }

        //两分钟原地高抬右腿
        if (lfzydgttyvallist != null && lfzydgttyvallist.stream().anyMatch(Objects::nonNull)) {
            lfzydgttyvaldata.put("name","两分钟原地高抬腿右腿");
            lfzydgttyvaldata.put("time",testtimelist);
            lfzydgttyvaldata.put("data",lfzydgttyvallist);
            dataList.add(lfzydgttyvaldata);
        }

        //安静心率
        if (ajxlvallist != null && ajxlvallist.stream().anyMatch(Objects::nonNull)) {
            ajxlvaldata.put("name","安静心率");
            ajxlvaldata.put("time",testtimelist);
            ajxlvaldata.put("data",ajxlvallist);
            dataList.add(ajxlvaldata);
        }

        //握力
        if (wolivallist != null && wolivallist.stream().anyMatch(Objects::nonNull)) {
            wolivaldata.put("name","握力");
            wolivaldata.put("time",testtimelist);
            wolivaldata.put("data",wolivallist);
            dataList.add(wolivaldata);
        }

        //纵跳
        if (zongtiaovallist != null && zongtiaovallist.stream().anyMatch(Objects::nonNull)) {
            zongtiaovaldata.put("name","纵跳");
            zongtiaovaldata.put("time",testtimelist);
            zongtiaovaldata.put("data",zongtiaovallist);
            dataList.add(zongtiaovaldata);
        }

        //俯卧撑
        if (fwcvallist != null && fwcvallist.stream().anyMatch(Objects::nonNull)) {
            fwcvaldata.put("name","俯卧撑");
            fwcvaldata.put("time",testtimelist);
            fwcvaldata.put("data",fwcvallist);
            dataList.add(fwcvaldata);
        }

        //跪卧撑
        if (gwcvallist != null && gwcvallist.stream().anyMatch(Objects::nonNull)) {
            gwcvaldata.put("name","跪卧撑");
            gwcvaldata.put("time",testtimelist);
            gwcvaldata.put("data",gwcvallist);
            dataList.add(gwcvaldata);
        }

        //一分钟仰卧起坐
        if (yfzywqzvallist != null && yfzywqzvallist.stream().anyMatch(Objects::nonNull)) {
            yfzywqzvaldata.put("name","一分钟仰卧起坐");
            yfzywqzvaldata.put("time",testtimelist);
            yfzywqzvaldata.put("data",yfzywqzvallist);
            dataList.add(yfzywqzvaldata);
        }

        //坐位体前屈
        if (zwtqqvallist != null && zwtqqvallist.stream().anyMatch(Objects::nonNull)) {
            zwtqqvaldata.put("name","坐位体前屈");
            zwtqqvaldata.put("time",testtimelist);
            zwtqqvaldata.put("data",zwtqqvallist);
            dataList.add(zwtqqvaldata);
        }

        //闭眼单脚斩
        if (bydjzlvallist != null && bydjzlvallist.stream().anyMatch(Objects::nonNull)) {
            bydjzlvaldata.put("name","闭眼单脚站立");
            bydjzlvaldata.put("time",testtimelist);
            bydjzlvaldata.put("data",bydjzlvallist);
            dataList.add(bydjzlvaldata);
        }

        //选择反应时
        if (xzfysvallist != null && xzfysvallist.stream().anyMatch(Objects::nonNull)) {
            xzfysvaldata.put("name","选择反应时");
            xzfysvaldata.put("time",testtimelist);
            xzfysvaldata.put("data",xzfysvallist);
            dataList.add(xzfysvaldata);
        }

        //背力
        if (beilivallist != null && beilivallist.stream().anyMatch(Objects::nonNull)) {
            beilivaldata.put("name","背力");
            beilivaldata.put("time",testtimelist);
            beilivaldata.put("data",beilivallist);
            dataList.add(beilivaldata);
        }

        //三十秒做站
        if (ssmzzvallist != null && ssmzzvallist.stream().anyMatch(Objects::nonNull)) {
            ssmzzvaldata.put("name","三十秒坐站");
            ssmzzvaldata.put("time",testtimelist);
            ssmzzvaldata.put("data",ssmzzvallist);
            dataList.add(ssmzzvaldata);
        }

        //立定跳远
        if (ldtyvallist != null && ldtyvallist.stream().anyMatch(Objects::nonNull)) {
            ldtyvaldata.put("name","立定跳远");
            ldtyvaldata.put("time",testtimelist);
            ldtyvaldata.put("data",ldtyvallist);
            dataList.add(ldtyvaldata);
        }

        //双脚连续跳
        if (sjlxtvallist != null && sjlxtvallist.stream().anyMatch(Objects::nonNull)) {
            sjlxtvaldata.put("name","双脚连续跳");
            sjlxtvaldata.put("time",testtimelist);
            sjlxtvaldata.put("data",sjlxtvallist);
            dataList.add(sjlxtvaldata);
        }

        //十五米绕障碍跑
        if (swmrzapvallist != null && swmrzapvallist.stream().anyMatch(Objects::nonNull)) {
            swmrzapvaldata.put("name","十五米绕障碍跑");
            swmrzapvaldata.put("time",testtimelist);
            swmrzapvaldata.put("data",swmrzapvallist);
            dataList.add(swmrzapvaldata);
        }

        //走平衡木
        if (zphmvallist != null && zphmvallist.stream().anyMatch(Objects::nonNull)) {
            System.out.println(zphmvallist);
            zphmvaldata.put("name","走平衡木");
            zphmvaldata.put("time",testtimelist);
            zphmvaldata.put("data",zphmvallist);
            dataList.add(zphmvaldata);
        }

        return dataList;
    }

    public boolean matches(SysUser user, String rawPassword)
    {
        return SecurityUtils.matchesPassword(rawPassword, user.getPassword());
    }
    public Map<String, Object> getTestOfPhysicalReport(float testage,List<TestCorporeityResult> testCorporeityResultList,String sex) {
        //重点关注list
        List<Map<String, Object>> FocusOnList = new ArrayList<Map<String, Object>>();
        //单项评价List
        List<Map<String, Object>> SingleEvaluationList = new ArrayList<Map<String, Object>>();
        //力量素质评价单项List
        List<TestCorporeityResult> suzhiList = new ArrayList<TestCorporeityResult>();
        //力量素质评价单项指标code liliangsuzhiList.add("13,14,15,16,17,23,24");
        List<String> liliangsuzhiList = new ArrayList<String>();
        liliangsuzhiList.add("13");
        liliangsuzhiList.add("14");
        liliangsuzhiList.add("15");
        liliangsuzhiList.add("16");
        liliangsuzhiList.add("17");
        liliangsuzhiList.add("23");
        liliangsuzhiList.add("24");

        for (TestCorporeityResult temp : testCorporeityResultList) {
            String indexcode = String.valueOf(temp.getItemcode());
            String grade = temp.getGrade();
            String testvalue = String.valueOf(temp.getTestvalue());
            Map<String, Object> gmtzdxpingjia = new HashMap<String, Object>();
            if (testage >= 3 && testage < 80) {
                try {
                    ////根据年龄段，评价等级，测试指标，查询出评价语、测试指标、指标分类gmtzdxpingjia
                    if (!indexcode.equals("100")) {
                        if (!grade.equals("不合格")) {
                            gmtzdxpingjia = homepageMapper.GetNewDXpingjia(testage, indexcode,testvalue,sex);
                            if(gmtzdxpingjia.get("pingjiayu") != null){
                                SingleEvaluationList.add(gmtzdxpingjia);
                            }
                        } else {
                            gmtzdxpingjia = homepageMapper.GetNewDXpingjia(testage, indexcode,testvalue,sex);
                            if(gmtzdxpingjia.get("pingjiayu") != null){
                                FocusOnList.add(gmtzdxpingjia);
                            }

                        }
                    }
                } catch (Exception e) {

                }
            }
            if (liliangsuzhiList.contains(indexcode)) {
                suzhiList.add(temp);
            }
        }
        //System.out.println("单项评价合格部分"+SingleEvaluationList);
        //System.out.println("单项评价重点关注部分"+FocusOnList);
        //力量素质指标不合格list ==》 UnqualifiedList
        List<String> UnqualifiedList = new ArrayList<String>();
        //力量素质指标总分
        int SumQuality = 0;
        String betty = "1";
        Map<String, Object> map = new HashMap<String, Object>();
        for (TestCorporeityResult temp : suzhiList) {
            if(temp.getGrade() != null){
                if (temp.getGrade().equals("优秀")) {
                    map.put("type", 4);
                } else if (temp.getGrade().equals("良好")) {
                    map.put("type", 3);
                } else if (temp.getGrade().equals("合格")) {
                    map.put("type", 2);
                } else if (temp.getGrade().equals("不合格")) {
                    map.put("type", 1);
                    betty = "2";
                    UnqualifiedList.add(temp.getZhibiaomingcheng());
                }
                SumQuality = SumQuality + (int) map.get("type");
            }

        }
        if (betty.equals("2")) {
            SumQuality = SumQuality - 1;
        }
        String UnqualifiedStr = "";
        if (UnqualifiedList.size() > 0) {
            UnqualifiedStr = StringUtils.join(UnqualifiedList, ",") + "指标不合格";
        }
        Map<String, Object> UnqualifiedMap = new HashMap<String, Object>();
        UnqualifiedMap.put("pingjiayu", UnqualifiedStr);
        //力量素质的code评价语：无老年人
        Map<String, Object> StrengthQualityFirst = new HashMap<String, Object>();
        Map<String, Object> StrengthQualitySupplement = new HashMap<String, Object>();
        Map<String, Object> StrengthQualityMap = new HashMap<String, Object>();
        if (testage < 60) {
            StrengthQualityMap = homepageMapper.Getliliangsuzhicode(testage, SumQuality);
            if (StrengthQualityMap != null && !StrengthQualityMap.equals("null")) {
                StrengthQualityFirst.put("pingjiayu", StrengthQualityMap.get("pingjiaqian"));
                StrengthQualitySupplement.put("pingjiayu", StrengthQualityMap.get("pingjiahou"));
            }
        }

        //zhibiaofenlei=形态，放在单项指标评价的首位
        List<Map<String, Object>> formList = new ArrayList<Map<String, Object>>();
        //zhibiaofenlei=机能，放在单项指标评价的二位
        List<Map<String, Object>> functionList = new ArrayList<Map<String, Object>>();
        //zhibiaofenlei=素质，参与力量素质评价的项目
        List<Map<String, Object>> qualityList = new ArrayList<Map<String, Object>>();
        //zhibiaofenlei=素质，不参与力量素质评价的项目，
        List<Map<String, Object>> qualityotherList = new ArrayList<Map<String, Object>>();

        for (Map<String, Object> temp : SingleEvaluationList) {
            String Zhibiaomingcheng = String.valueOf(temp.get("ceshizhibiao"));
            if (temp.get("fenleicode").equals("3")) {
                if (liliangsuzhiList.contains(Zhibiaomingcheng)) {
                    qualityList.add(temp);
                } else {
                    qualityotherList.add(temp);
                }
            } else if (temp.get("fenleicode").equals("1")) {
                formList.add(temp);
            } else if (temp.get("fenleicode").equals("2")) {
                functionList.add(temp);
            }

        }
        if (testage < 60) {
            if (StrengthQualityMap != null && !StrengthQualityMap.equals("null")) {

                qualityList.add(0, StrengthQualityFirst);
                qualityList.add(UnqualifiedMap);
                qualityList.add(StrengthQualitySupplement);
            }

        }

        //把形态、机能、素质的单项指标评价合并成一个ListMap，pingjiayu就是要显示的内容
        formList.addAll(functionList);
        formList.addAll(qualityList);
        formList.addAll(qualityotherList);
        //运动处方map包括个人指标单项评价、重点关注
        Map<String, Object> ExercisePrescriptionMap = new HashMap<String, Object>();
        ExercisePrescriptionMap.put("FocusOnList", FocusOnList);
        ExercisePrescriptionMap.put("SingleEvaluationList", formList);

        return ExercisePrescriptionMap;
    }

}
